package calvin.study.rabbitmq.advanced.confirm;

import java.io.IOException;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ConfirmListener;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import calvin.study.rabbitmq.base.RabbitMqBase;

public class Producer extends RabbitMqBase {

	public static void main(String[] args) throws Exception {
		// 1 创建ConnectionFactory
		ConnectionFactory connectionFactory = getConnectionFactory();
		Connection connection = null;
		Channel channel = null;
		try {
			// 2 获取Connection
			connection = connectionFactory.newConnection();
			// 3 通过Connection创建一个新的Channel
			channel = connection.createChannel();
			// 4 指定我们的消息投递模式: 消息的确认模式
			channel.confirmSelect();
			String exchangeName = "confirm_topic_exchange";
			String routingKey = "confirm.save";

			// 5 发送一条消息
			String msg = "Hello RabbitMQ Send confirm message!";
			channel.basicPublish(exchangeName, routingKey, null, msg.getBytes());

			// 6 添加一个确认监听
			channel.addConfirmListener(new ConfirmListener() {
				// 失败
				// deliveryTag 消息唯一标签
				@Override
				public void handleNack(long deliveryTag, boolean multiple) throws IOException {
					System.out.println("-------no ack!-----------");
				}

				// 成功
				@Override
				public void handleAck(long deliveryTag, boolean multiple) throws IOException {
					System.out.println("-------ack!-----------");
				}
			});
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			//channel.close();
			//connection.close();
		}

	}
}
